package com.herench.bunch.commons.support;

import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v24.segment.MSA;
import com.herench.bunch.commons.utils.DataFormatUtils;
import com.herench.bunch.commons.utils.IdGenerator;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ThinkPad on 2017/8/14.
 */
public class ResponseMessage {


    public static final String RES_CODE="code";
    public static final String MESSAGE_IN="messageIn";
    public static final String MESSAGE_OUT="messageOut";
    public static final String MESSAGE="message";


    private final String id;
    private final String reqId;//对应的请求报文的id
    private Map<String, Object> data;//报文体参数
    private String messageIn;//结果说明（内部）
    private String messageOut;  //结果说明（外部）
    private String message;//返回结果
    private String code;//执行状态码 0 正常
    private Message hl7Message;
    private String reciver;


    public ResponseMessage(String reqId) {
        Assert.notNull(reqId, "reqId 参数不能为空.");
        String id = IdGenerator.getUUID();
        this.id = id;
        this.reqId = reqId;
    }

    public void setDataAttribute(String property,Object val){
        Assert.notNull(property,"属性不能为空.");
        Assert.notNull(val,"属性值不能为空.");
        if (this.data==null){
            this.data = new HashMap<>();
        }
        if (val instanceof Date){
            val = DataFormatUtils.getTimeString((Date) val);
        }
        this.data.put(property,val);
    }

    public String getMessageIn() {
        return messageIn;
    }

    public void setMessageIn(String messageIn) {
        this.messageIn = messageIn;
    }

    public String getMessageOut() {
        return messageOut;
    }

    public void setMessageOut(String messageOut) {
        this.messageOut = messageOut;
    }

    public Map<String, Object> getData() {
        if (data==null){
            this.data = new HashMap<>();
        }
        return data;
    }

    public String getId() {
        return id;
    }

    public String getReqId() {
        return reqId;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Message getHl7Message() {
        return hl7Message;
    }

    public void setHl7Message(Message hl7Message) throws Exception{
//        MSA msa = (MSA)hl7Message.get("MSA");
//        if (msa!=null ){
//            if (msa.getErrorCondition().getCe1_Identifier().getValue().equals("1")){
//                setCode("0");
//            }else {
//                setCode(msa.getErrorCondition().getCe1_Identifier().getValue());
//            }
//            setMessage(msa.getErrorCondition().getCe2_Text().getValue());
//        }
        this.hl7Message = hl7Message;
    }

    public void setData(Map<String, Object> data) {
        if (data!=null && !data.isEmpty()){
            for (Map.Entry<String,Object> entry:data.entrySet()){
                if (entry.getValue() instanceof Date){
                    entry.setValue(DataFormatUtils.getTimeString((Date) entry.getValue()));
                }
            }
        }
        this.data = data;
    }

    public String getReciver() {
        return reciver;
    }

    public void setReciver(String reciver) {
        this.reciver = reciver;
    }

    @Override
    public String toString() {
        return "ResponseMessage{" +
                "id='" + id + '\'' +
                ", reqId='" + reqId + '\'' +
                ", data=" + data +
                ", messageIn='" + messageIn + '\'' +
                ", messageOut='" + messageOut + '\'' +
                ", message='" + message + '\'' +
                ", code='" + code + '\'' +
                '}';
    }
}
